package com.xhwl.centiotdata.handler.account.impl;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.handler.account.AccountFactory;
import com.xhwl.centiotdata.handler.account.AccountHandler;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationFactory;
import com.xhwl.centiotdata.handler.pedestrian.PedestrianIntegrationHandler;
import com.xhwl.centiotdata.pojo.dto.pedestrian.PedestrianIntegrationConfigDTO;
import com.xhwl.centiotdata.service.cent.IAuthAccountCentService;
import com.xhwl.common.dto.auth.PedestrianAuthDTO;
import com.xhwl.common.dto.cent.sdata.AccountDTO;
import com.xhwl.common.enums.ProductEnum;
import com.xhwl.common.pojo.cent.auth.AuthAccountMapping;
import com.xhwl.common.pojo.cent.device.DeviceProjSupplierCategory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author tangwei
 * @description
 * @create 2022-09-29 17:36
 */
@Slf4j
@Service
public class AccountHuaWeiIotHandler extends AccountHandler {

    @Autowired
    private IAuthAccountCentService iAuthPedestrianCentService;

    @Override
    public void afterPropertiesSet() throws Exception {
        AccountFactory.register(ProductEnum.HUAWEI_IOTDA.getId(), this);
    }

    @Override
    public void delete(AccountDTO accountDTO, Integer projectId, DeviceProjSupplierCategory integrationConfig) {
        if (null != integrationConfig) {
            //查询关联表
            AuthAccountMapping authAccount = iAuthPedestrianCentService.query(projectId, accountDTO.getId(), integrationConfig.getProductId());
            if (null != authAccount) {
                handleDelete(authAccount, projectId, integrationConfig);
                iAuthPedestrianCentService.deleteAccountMapping(projectId, accountDTO.getId(), authAccount.getOpenId(), integrationConfig.getProductId());
            }
        }
    }

    private void handleDelete(AuthAccountMapping authAccount, Integer projectId, DeviceProjSupplierCategory integrationConfig) {
        if (null != integrationConfig) {
            try {
                JSONObject properties = integrationConfig.getProperties();
                Integer productId = integrationConfig.getProductId();
                PedestrianAuthDTO pedestrianAuthDTO = new PedestrianAuthDTO();
                pedestrianAuthDTO.setProjectId(projectId);
                pedestrianAuthDTO.setProductId(productId);
                pedestrianAuthDTO.setSourceId(authAccount.getAccountId());
                pedestrianAuthDTO.setUserOpenId(authAccount.getOpenId());
                PedestrianIntegrationConfigDTO config = new PedestrianIntegrationConfigDTO();
                config.setIpAndPort(properties.getString("ipAndPort"));
                config.setAk(properties.getString("ak"));
                config.setSk(properties.getString("sk"));
                config.setProductId(productId);
                config.setLoginName(properties.getString("loginName"));
                config.setPassword(properties.getString("password"));
                config.setProjectId(projectId);
                PedestrianIntegrationHandler handler = PedestrianIntegrationFactory.getInvokeStrategy(productId);
                if (handler != null) {
                    log.info("同步捷顺平台权限删除");
                    handler.deleteAuth(config, pedestrianAuthDTO);
                }
            } catch (Exception e) {
                log.error("账号信息：{}", authAccount);
                log.error("配置信息：{}", integrationConfig);
                log.error("同步捷顺平台异常", e);
            }
        }
    }

}
